home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / forth / pfe-0.000 / pfe-0 / pfe-0.9.13 / src / curses.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  5.1 KB  |  218 lines

  1. /*
  2.  * This file is part of the portable Forth environment written in ANSI C.
  3.  * Copyright (C) 1995  Dirk Uwe Zoller
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  * See the GNU Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * This file is version 0.9.13 of 17-July-95
  20.  * Check for the latest version of this package via anonymous ftp at
  21.  *    roxi.rz.fht-mannheim.de:/pub/languages/forth/pfe-VERSION.tar.gz
  22.  * or    sunsite.unc.edu:/pub/languages/forth/pfe-VERSION.tar.gz
  23.  * or    ftp.cygnus.com:/pub/forth/pfe-VERSION.tar.gz
  24.  *
  25.  * Please direct any comments via internet to
  26.  *    duz@roxi.rz.fht-mannheim.de.
  27.  * Thank You.
  28.  */
  29. /*
  30.  * curses.c ---    Experimental terminal driver for UNIX-like systems
  31.  *        using the curses library.
  32.  *
  33.  *        This driver works with Linux and the ncurses library.
  34.  *        It doesn't work any better than the termcap.c driver.
  35.  *        To use it change makefile as follows:
  36.  *
  37.  *            OPTS=    -I/usr/include/ncurses
  38.  *            LIBS=    -lncurses
  39.  *            TERM_O=    curses.o
  40.  * (duz 17May94)
  41.  */
  42.  
  43. #include "forth.h"
  44. #include "term.h"
  45.  
  46. #undef TRUE
  47. #undef FALSE
  48.  
  49. #include <limits.h>
  50. #include <sys/ioctl.h>        /* ioctl(), TIOCGWINSZ */
  51.  
  52. #ifdef Linux
  53. # include <ncurses.h>
  54. #else
  55. # include <curses.h>
  56. #endif
  57.  
  58. #include "missing.h"
  59.  
  60. #define SEQ(X)    { '\033', (Byte)((X) >> CHAR_BIT), (Byte)(X), 0 }
  61.  
  62. static Byte
  63. rks [][4] =            /* what function keys send */
  64. {
  65.   SEQ (KEY_F(1)),    SEQ (KEY_F(2)),
  66.   SEQ (KEY_F(3)),    SEQ (KEY_F(4)),
  67.   SEQ (KEY_F(5)),    SEQ (KEY_F(6)),
  68.   SEQ (KEY_F(7)),    SEQ (KEY_F(8)),
  69.   SEQ (KEY_F(9)),    SEQ (KEY_F(10)),
  70.   SEQ (KEY_LEFT),    SEQ (KEY_RIGHT),
  71.   SEQ (KEY_UP),        SEQ (KEY_DOWN),
  72.   SEQ (KEY_HOME),    SEQ (KEY_END),
  73.   SEQ (KEY_PPAGE),    SEQ (KEY_NPAGE),
  74.   SEQ (KEY_BACKSPACE),    SEQ (KEY_DC),
  75.   SEQ (KEY_IC),        SEQ (KEY_IC),
  76.   SEQ (KEY_IL),        SEQ (KEY_EOL),
  77.   SEQ (KEY_DL),        SEQ (KEY_CLEAR)
  78. };
  79.  
  80. Byte *rawkey_string [] =
  81. {
  82.   rks [ 0], rks [ 1], rks [ 2], rks [ 3], rks [ 4],
  83.   rks [ 5], rks [ 6], rks [ 7], rks [ 8], rks [ 9],
  84.   rks [10], rks [11], rks [12], rks [13], rks [14],
  85.   rks [15], rks [16], rks [17], rks [18], rks [19],
  86.   rks [20], rks [21], rks [22], rks [23], rks [24],
  87.   rks [25]
  88. };
  89.  
  90. void show_control_strings() {}
  91. void show_rawkey_strings () {}
  92.  
  93. int tty_interrupt_key (char ch)        { return 0; }
  94. void interactive_terminal (void)    { fixterm (); }
  95. void system_terminal (void)        { resetterm (); }
  96.  
  97. int
  98. prepare_terminal (void)
  99. {
  100.   initscr ();
  101.   cbreak ();
  102.   nonl ();
  103.   noecho ();
  104.   idlok (stdscr, TRUE);
  105.   scrollok (stdscr, TRUE);
  106.   meta (stdscr, TRUE);
  107.   keypad (stdscr, TRUE);
  108.   nodelay (stdscr, FALSE);
  109.   saveterm ();
  110.   refresh ();
  111.   return 1;
  112. }
  113.  
  114. #ifdef TIOCGWINSZ
  115. void
  116. query_winsize (void)
  117. {
  118.   struct winsize size;
  119.  
  120.   if (ioctl (1, TIOCGWINSZ, (char *) &size) >= 0)
  121.     {
  122.       rows = size.ws_row;
  123.       cols = size.ws_col;
  124.       xmax = size.ws_xpixel;
  125.       ymax = size.ws_ypixel;
  126.     }
  127. }
  128.  
  129. #else
  130. void query_winsize (void) {} 
  131. #endif
  132.  
  133. static Byte nextch[3];
  134. static int nnext = 0;
  135.  
  136. static int
  137. key (void)
  138. {
  139.   int c;
  140.  
  141.   c = getch ();
  142.   if (c == -1)
  143.     return 0;
  144.   if (c < 0x100)
  145.     {
  146.       nextch[nnext++] = c;
  147.     }
  148.   else
  149.     {
  150.       nextch[nnext++] = c;
  151.       nextch[nnext++] = c >> CHAR_BIT;
  152.       nextch[nnext++] = '\033';
  153.     }
  154.   return 1;
  155. }
  156.  
  157. int
  158. c_keypressed (void)
  159. {
  160.   refresh ();
  161.   if (nnext)
  162.     return 1;
  163.   nodelay (stdscr, TRUE);
  164.   key ();
  165.   nodelay (stdscr, FALSE);
  166.   return nnext != 0;
  167. }
  168.  
  169. int
  170. c_getkey (void)
  171. {
  172.   refresh ();
  173.   if (nnext || key ())
  174.     return nextch[--nnext];
  175.   else
  176.     return -1;
  177. }
  178.  
  179. void 
  180. c_putc_noflush (char c)
  181. {
  182.   addch (c);
  183. }
  184. void c_flush ()            { refresh (); }
  185. void c_putc (char c)        { addch (c); refresh (); }
  186. void c_puts (const char *s)    { addstr ((char *)s); refresh (); }
  187.  
  188. void c_gotoxy (int x, int y)    { move (y, x); }
  189. void c_wherexy (int *x, int *y)    { getyx (stdscr, *y, *x); }
  190.  
  191. static void
  192. addxy (int x, int y)
  193. {
  194.   int col, row;
  195.   getyx (stdscr, row, col);
  196.   move (row + y, col + x);
  197. }
  198.  
  199. void c_goleft (void)        { addxy (-1,  0); }
  200. void c_goright (void)        { addxy ( 1,  0); }
  201. void c_goup (void)        { addxy ( 0, -1); }
  202. void c_godown (void)        { addxy ( 0,  1); }
  203.  
  204. void c_clrscr (void)        { clear (); refresh (); }
  205. void c_home (void)        { move (0, 0); }
  206. void c_clreol (void)        { clrtoeol (); }
  207. void c_clrdown (void)        { clrtobot (); }
  208. void c_bell (void)        { beep (); }
  209.  
  210. void c_standout_on (void)        { standout (); }
  211. void c_standout_off (void)    { standend (); }
  212. void c_bright (void)        { attron (A_BOLD); }
  213. void c_reverse (void)        { attron (A_REVERSE); }
  214. void c_blinking (void)        { attron (A_BLINK); }
  215. void c_normal (void)        { attrset (A_NORMAL); }
  216. void c_underline_on (void)    { attron (A_UNDERLINE); }
  217. void c_underline_off (void)    { attroff (A_UNDERLINE); }
  218.